import decimal
import random
import time

import requests
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker


class RetailOrderSharing:

    token = ""
    goodSku = None

    def __init__(self):
        self.engine = create_engine("mysql+pymysql://sql_love_back:WHWn7fGkWy33XpHy@120.78.221.48:3306/sql_love_back", max_overflow=10)
        self.session = sessionmaker(bind=self.engine)()

    # 核算代理分润
    def check_agencys_sharing(self, retailAgentId, retailGoodsSkusPrice, orderId, crossStore, bindStoreId):
        print("retailAgentId：", retailAgentId)
        self.session.commit()
        retail_agent = self.session.execute(
            "select user_id, parent_id from retail_agents where id = :agentId limit 1",
            params={"agentId": retailAgentId}).first()
        print("retail_agent：", retail_agent)

        self.session.commit()
        change_price = self.session.execute(
            "select change_price from retail_user_frozen_price1_records where user_id = :userId and target_id = :targetId limit 1",
            params={"userId": retail_agent[0], "targetId": orderId}).first()

        print("crossStore：", crossStore)
        if change_price[0] != retailGoodsSkusPrice:
            print("{}营业额计算错误，预期{}，实际{}".format(retail_agent[0], retailGoodsSkusPrice, change_price[0]))

        if crossStore == False:
            if retailAgentId == 1:
                print("走没走。。。。")
                return None

        if crossStore == True:
            print("crossStore：", crossStore)
            if retailAgentId == 1 or retail_agent[1] == 0:
                crossStore = False
                self.session.commit()
                retail_agent_id = self.session.execute(
                    "select retail_agent_id from retail_stores where id = :storeId limit 1",
                    params={"storeId": bindStoreId}).first()
                self.check_agencys_sharing(retail_agent_id[0], retailGoodsSkusPrice / 2,
                                           orderId, crossStore, bindStoreId)

        # 防止crossStore未修改成False
        if retailAgentId == 1:
            return

        if retail_agent[1] > 0:
            self.check_agencys_sharing(retail_agent[1], retailGoodsSkusPrice / 2, orderId, crossStore, bindStoreId)


    # 核算上级分润
    def check_members_sharing(self, userId, orderId, amount, subRatioList, retailGoodsSkusPrice, retailSets, ordinary, union, partner, crossStore, bindStoreId, unboundStoreId, userLevel):
        self.session.commit()
        referee_user = self.session.execute("select referee_user_id from retail_users ru where ru.user_id = :userId limit 1", params={"userId": userId}).first()
        print("referee_user：", referee_user)

        if referee_user == None:
            return

        self.session.commit()
        user_vip_level = self.session.execute("select vip_level from retail_vips where user_id = :userId limit 1",
                                         params={"userId": referee_user[0]}).first()
        print("user_vip_level：", user_vip_level)

        # 非跨店
        if crossStore == False:
            if userLevel != None and userLevel[0] == 3:
                print("userLevel：", userLevel)
                self.session.commit()
                retail_agent_id = self.session.execute(
                    "select retail_agent_id from retail_stores where id = :storeId limit 1",
                    params={"storeId": bindStoreId}).first()
                self.check_agencys_sharing(retail_agent_id[0], retailGoodsSkusPrice * retailSets[3] / 100 / 2, orderId,
                                        crossStore, bindStoreId)
                return

            if user_vip_level == None:
                if ordinary == 1 and referee_user[0] > 0:
                    if union == 1 and partner == 1:
                        self.session.commit()
                        change_credit = self.session.execute("select change_credit from retail_user_frozen_credit_records where user_id = :userId and target_id = :targetId limit 1",
                                                      params={"userId": referee_user[0], "targetId":orderId}).first()
                        if change_credit[0] != amount * retailSets[0]:
                            print("{}积分计算错误，预期{}，实际{}".format(referee_user[0], amount * retailSets[0], change_credit[0]))

                self.check_members_sharing(referee_user[0], orderId, amount, subRatioList, retailGoodsSkusPrice, retailSets, ordinary + 1, union, partner, crossStore, bindStoreId, unboundStoreId, userLevel)

            else:
                if user_vip_level[0] == 1:
                    if union == 1:
                        if partner == 1:
                            self.session.commit()
                            change_price = self.session.execute(
                                "select change_price from retail_user_frozen_price3_records where user_id = :userId and target_id = :targetId limit 1",
                                params={"userId": referee_user[0], "targetId": orderId}).first()
                            if change_price[0] != retailGoodsSkusPrice * subRatioList[0] / 100:
                                print("{}零售利润计算错误，预期{}，实际{}".format(referee_user[0], retailGoodsSkusPrice * subRatioList[0] / 100, change_price[0]))

                    self.check_members_sharing(referee_user[0], orderId, amount, subRatioList, retailGoodsSkusPrice, retailSets, ordinary, union + 1, partner, crossStore, bindStoreId, unboundStoreId, userLevel)

                elif user_vip_level[0] == 2:
                    if partner == 1:
                        if union == 1:
                            self.session.commit()
                            change_price = self.session.execute(
                                "select change_price from retail_user_frozen_price3_records where user_id = :userId and target_id = :targetId limit 1",
                                params={"userId": referee_user[0], "targetId": orderId}).first()
                            if change_price[0] != retailGoodsSkusPrice * subRatioList[1] / 100:
                                print("{}零售利润计算错误，预期{}，实际{}".format(referee_user[0], retailGoodsSkusPrice * subRatioList[1] / 100, change_price[0]))
                        else:
                            if userLevel != None and userLevel[0] == 1:
                                self.session.commit()
                                change_price = self.session.execute(
                                    "select change_price from retail_user_frozen_price3_records where user_id = :userId and target_id = :targetId limit 1",
                                    params={"userId": referee_user[0], "targetId": orderId}).first()
                                if change_price[0] != retailGoodsSkusPrice * (subRatioList[1] - subRatioList[0]) / 100:
                                    print("{}零售利润计算错误，预期{}，实际{}".format(referee_user[0], retailGoodsSkusPrice * (
                                                subRatioList[1] - subRatioList[0]) / 100, change_price[0]))
                            else:
                                self.session.commit()
                                change_price = self.session.execute(
                                    "select change_price from retail_user_frozen_price4_records where user_id = :userId and target_id = :targetId limit 1",
                                    params={"userId": referee_user[0], "targetId": orderId}).first()
                                if change_price[0] != retailGoodsSkusPrice * (subRatioList[1] - subRatioList[0]) / 100:
                                    print("{}分润差额1计算错误，预期{}，实际{}".format(referee_user[0], retailGoodsSkusPrice * (subRatioList[1] - subRatioList[0]) / 100, change_price[0]))

                    self.check_members_sharing(referee_user[0], orderId, amount, subRatioList, retailGoodsSkusPrice, retailSets, ordinary, union, partner + 1, crossStore, bindStoreId, unboundStoreId, userLevel)

                elif user_vip_level[0] == 3:
                    if union == 1 and partner == 1:
                        self.session.commit()
                        change_price = self.session.execute(
                            "select change_price from retail_user_frozen_price3_records where user_id = :userId and target_id = :targetId limit 1",
                            params={"userId": referee_user[0], "targetId": orderId}).first()
                        if change_price[0] != retailGoodsSkusPrice * (100 - retailSets[3]) / 100:
                            print("{}零售利润计算错误，预期{}，实际{}".format(referee_user[0], retailGoodsSkusPrice * (100 - retailSets[3]) / 100, change_price[0]))
                    elif union > 1 and partner == 1:
                        if userLevel != None and userLevel[0] == 1:
                            self.session.commit()
                            change_price = self.session.execute(
                                "select change_price from retail_user_frozen_price3_records where user_id = :userId and target_id = :targetId limit 1",
                                params={"userId": referee_user[0], "targetId": orderId}).first()
                            if change_price[0] != retailGoodsSkusPrice * (100 - retailSets[3] - subRatioList[0]) / 100:
                                print("{}零售利润计算错误，预期{}，实际{}".format(referee_user[0], retailGoodsSkusPrice * (
                                            100 - retailSets[3] - subRatioList[0]) / 100, change_price[0]))
                        else:
                            self.session.commit()
                            change_price = self.session.execute(
                                "select change_price from retail_user_frozen_price4_records where user_id = :userId and target_id = :targetId limit 1",
                                params={"userId": referee_user[0], "targetId": orderId}).first()
                            if change_price[0] != retailGoodsSkusPrice * (100 - retailSets[3] - subRatioList[0]) / 100:
                                print("{}分润差额1计算错误，预期{}，实际{}".format(referee_user[0], retailGoodsSkusPrice * (100 - retailSets[3] - subRatioList[0]) / 100, change_price[0]))
                    elif partner > 1:
                        if userLevel != None:
                            if userLevel[0] == 2:
                                self.session.commit()
                                change_price = self.session.execute(
                                    "select change_price from retail_user_frozen_price3_records where user_id = :userId and target_id = :targetId limit 1",
                                    params={"userId": referee_user[0], "targetId": orderId}).first()
                                if change_price[0] != retailGoodsSkusPrice * (100 - retailSets[3] - subRatioList[1]) / 100:
                                    print("{}零售利润计算错误，预期{}，实际{}".format(referee_user[0], retailGoodsSkusPrice * (
                                                100 - retailSets[3] - subRatioList[1]) / 100, change_price[0]))
                            elif userLevel[0] == 1:
                                self.session.commit()
                                change_price = self.session.execute(
                                    "select change_price from retail_user_frozen_price4_records where user_id = :userId and target_id = :targetId limit 1",
                                    params={"userId": referee_user[0], "targetId": orderId}).first()
                                if change_price[0] != retailGoodsSkusPrice * (
                                        100 - retailSets[3] - subRatioList[1]) / 100:
                                    print("{}分润差额1计算错误，预期{}，实际{}".format(referee_user[0], retailGoodsSkusPrice * (
                                            100 - retailSets[3] - subRatioList[1]) / 100, change_price[0]))
                        else:
                            if union == 1:
                                self.session.commit()
                                change_price = self.session.execute(
                                    "select change_price from retail_user_frozen_price4_records where user_id = :userId and target_id = :targetId limit 1",
                                    params={"userId": referee_user[0], "targetId": orderId}).first()
                                if change_price[0] != retailGoodsSkusPrice * (
                                        100 - retailSets[3] - subRatioList[1]) / 100:
                                    print("{}分润差额1计算错误，预期{}，实际{}".format(referee_user[0], retailGoodsSkusPrice * (
                                                100 - retailSets[3] - subRatioList[1]) / 100, change_price[0]))
                            else:
                                self.session.commit()
                                change_price = self.session.execute(
                                    "select change_price from retail_user_frozen_price5_records where user_id = :userId and target_id = :targetId limit 1",
                                    params={"userId": referee_user[0], "targetId": orderId}).first()
                                if change_price[0] != retailGoodsSkusPrice * (100 - retailSets[3] - subRatioList[1]) / 100:
                                    print("{}分润差额2计算错误，预期{}，实际{}".format(referee_user[0], retailGoodsSkusPrice * (100 - retailSets[3] - subRatioList[1]) / 100, change_price[0]))

                    self.session.commit()
                    retail_agent_id = self.session.execute(
                        "select retail_agent_id from retail_stores where id = :storeId limit 1",
                        params={"storeId": bindStoreId}).first()
                    self.check_agencys_sharing(retail_agent_id[0], retailGoodsSkusPrice * retailSets[3] / 100 / 2, orderId, crossStore, bindStoreId)

        # 跨店
        else:
            if userLevel != None and userLevel[0] == 3:
                self.session.commit()
                unbound_store_change_price = self.session.execute(
                    "select rufpr.change_price, rs.user_id\
                            from retail_stores rs\
                            inner join retail_user_frozen_price10_records rufpr on rs.user_id = rufpr.user_id\
                            where rs.id = :unboundStoreId and rufpr.target_id = :targetId limit 1",
                    params={"unboundStoreId": unboundStoreId, "targetId": orderId}).first()
                print("unbound_store_change_price：", unbound_store_change_price)
                if unbound_store_change_price[0] != retailGoodsSkusPrice * retailSets[2] / 100:
                    print("{}跨店收益计算错误，预期{}，实际{}".format(unbound_store_change_price[1],
                                                        retailGoodsSkusPrice * retailSets[2] / 100,
                                                        unbound_store_change_price[0]))

                self.session.commit()
                retail_agent_id = self.session.execute(
                    "select retail_agent_id from retail_stores where id = :storeId limit 1",
                    params={"storeId": unboundStoreId}).first()
                self.check_agencys_sharing(retail_agent_id[0], retailGoodsSkusPrice * retailSets[3] / 100 / 2,
                                           orderId, crossStore, bindStoreId)
                return

            if user_vip_level == None:
                self.check_members_sharing(referee_user[0], orderId, amount, subRatioList, retailGoodsSkusPrice,
                                           retailSets, ordinary + 1, union, partner, crossStore, bindStoreId,
                                           unboundStoreId, userLevel)

            else:
                if user_vip_level[0] == 1:
                    self.check_members_sharing(referee_user[0], orderId, amount, subRatioList, retailGoodsSkusPrice,
                                               retailSets, ordinary, union + 1, partner, crossStore, bindStoreId,
                                               unboundStoreId, userLevel)

                elif user_vip_level[0] == 2:
                    self.check_members_sharing(referee_user[0], orderId, amount, subRatioList, retailGoodsSkusPrice,
                                               retailSets, ordinary, union, partner + 1, crossStore, bindStoreId,
                                               unboundStoreId, userLevel)
                elif user_vip_level[0] == 3:
                    self.session.commit()
                    unbound_store_change_price = self.session.execute(
                        "select rufpr.change_price, rs.user_id\
                                from retail_stores rs\
                                inner join retail_user_frozen_price10_records rufpr on rs.user_id = rufpr.user_id\
                                where rs.id = :unboundStoreId and rufpr.target_id = :targetId limit 1",
                        params={"unboundStoreId": unboundStoreId, "targetId": orderId}).first()
                    print("unbound_store_change_price：", unbound_store_change_price)
                    if unbound_store_change_price[0] != retailGoodsSkusPrice * retailSets[2] / 100:
                        print("{}跨店收益计算错误，预期{}，实际{}".format(unbound_store_change_price[1],
                                                            retailGoodsSkusPrice * retailSets[2] / 100,
                                                            unbound_store_change_price[0]))

                    if userLevel == None:
                        self.session.commit()
                        change_price = self.session.execute(
                            "select change_price from retail_user_frozen_price3_records where user_id = :userId and target_id = :targetId limit 1",
                            params={"userId": referee_user[0], "targetId": orderId}).first()
                        if change_price[0] != retailGoodsSkusPrice * (100 - retailSets[3] - retailSets[2]) / 100:
                            print("{}零售利润计算错误，预期{}，实际{}".format(referee_user[0],
                                                                retailGoodsSkusPrice * (
                                                                            100 - retailSets[3] - retailSets[2]) / 100,
                                                                change_price[0]))
                    else:
                        if userLevel[0] == 1:
                            self.session.commit()
                            change_price = self.session.execute(
                                "select change_price from retail_user_frozen_price3_records where user_id = :userId and target_id = :targetId limit 1",
                                params={"userId": referee_user[0], "targetId": orderId}).first()
                            if change_price[0] != retailGoodsSkusPrice * (100 - retailSets[3] - retailSets[2] - subRatioList[0]) / 100:
                                print("{}零售利润计算错误，预期{}，实际{}".format(referee_user[0],
                                                                    retailGoodsSkusPrice * (
                                                                                100 - retailSets[3] - retailSets[
                                                                            2] - subRatioList[0]) / 100,
                                                                    change_price[0]))
                        elif userLevel[0] == 2:
                            self.session.commit()
                            change_price = self.session.execute(
                                "select change_price from retail_user_frozen_price3_records where user_id = :userId and target_id = :targetId limit 1",
                                params={"userId": referee_user[0], "targetId": orderId}).first()
                            if change_price[0] != retailGoodsSkusPrice * (100 - retailSets[3] - retailSets[2] - subRatioList[1]) / 100:
                                print("{}零售利润计算错误，预期{}，实际{}".format(referee_user[0],
                                                                    retailGoodsSkusPrice * (
                                                                                100 - retailSets[3] - retailSets[
                                                                            2] - subRatioList[1]) / 100,
                                                                    change_price[0]))

                    # if union == 1 and partner == 1:
                    #     self.session.commit()
                    #     change_price = self.session.execute(
                    #         "select change_price from retail_user_frozen_price3_records where user_id = :userId and target_id = :targetId limit 1",
                    #         params={"userId": referee_user[0], "targetId": orderId}).first()
                    #     if change_price[0] != retailGoodsSkusPrice * (100 - retailSets[3] - retailSets[2]) / 100:
                    #         print("{}零售利润计算错误，预期{}，实际{}".format(referee_user[0],
                    #                                             retailGoodsSkusPrice * (100 - retailSets[3] - retailSets[2]) / 100,
                    #                                             change_price[0]))
                    # elif union > 1 and partner == 1:
                    #     if userLevel != None and userLevel[0] == 1:
                    #         self.session.commit()
                    #         change_price = self.session.execute(
                    #             "select change_price from retail_user_frozen_price3_records where user_id = :userId and target_id = :targetId limit 1",
                    #             params={"userId": referee_user[0], "targetId": orderId}).first()
                    #         if change_price[0] != retailGoodsSkusPrice * (
                    #                 100 - retailSets[3] - retailSets[2] - subRatioList[0]) / 100:
                    #             print("{}零售利润计算错误，预期{}，实际{}".format(referee_user[0], retailGoodsSkusPrice * (
                    #                         100 - retailSets[3] - retailSets[2] - subRatioList[0]) / 100,
                    #                                                  change_price[0]))
                    #     else:
                    #         self.session.commit()
                    #         change_price = self.session.execute(
                    #             "select change_price from retail_user_frozen_price4_records where user_id = :userId and target_id = :targetId limit 1",
                    #             params={"userId": referee_user[0], "targetId": orderId}).first()
                    #         if change_price[0] != retailGoodsSkusPrice * (100 - retailSets[3] - retailSets[2] - subRatioList[0]) / 100:
                    #             print("{}分润差额1计算错误，预期{}，实际{}".format(referee_user[0], retailGoodsSkusPrice * (100 - retailSets[3] - retailSets[2] - subRatioList[0]) / 100, change_price[0]))
                    # elif partner > 1:
                    #     if userLevel != None:
                    #         if userLevel[0] == 2:
                    #             self.session.commit()
                    #             change_price = self.session.execute(
                    #                 "select change_price from retail_user_frozen_price3_records where user_id = :userId and target_id = :targetId limit 1",
                    #                 params={"userId": referee_user[0], "targetId": orderId}).first()
                    #             if change_price[0] != retailGoodsSkusPrice * (
                    #                     100 - retailSets[3] - retailSets[2] - subRatioList[1]) / 100:
                    #                 print("{}零售利润计算错误，预期{}，实际{}".format(referee_user[0], retailGoodsSkusPrice * (
                    #                             100 - retailSets[3] - retailSets[2] - subRatioList[1]) / 100,
                    #                                                      change_price[0]))
                    #         elif userLevel[0] == 1:
                    #             self.session.commit()
                    #             change_price = self.session.execute(
                    #                 "select change_price from retail_user_frozen_price4_records where user_id = :userId and target_id = :targetId limit 1",
                    #                 params={"userId": referee_user[0], "targetId": orderId}).first()
                    #             if change_price[0] != retailGoodsSkusPrice * (
                    #                     100 - retailSets[3] - retailSets[2] - subRatioList[1]) / 100:
                    #                 print("{}分润差额1计算错误，预期{}，实际{}".format(referee_user[0], retailGoodsSkusPrice * (
                    #                         100 - retailSets[3] - retailSets[2] - subRatioList[1]) / 100,
                    #                                                     change_price[0]))
                    #     else:
                    #         if union == 1:
                    #             self.session.commit()
                    #             change_price = self.session.execute(
                    #                 "select change_price from retail_user_frozen_price4_records where user_id = :userId and target_id = :targetId limit 1",
                    #                 params={"userId": referee_user[0], "targetId": orderId}).first()
                    #             if change_price[0] != retailGoodsSkusPrice * (
                    #                     100 - retailSets[3] - retailSets[2] - subRatioList[1]) / 100:
                    #                 print("{}分润差额1计算错误，预期{}，实际{}".format(referee_user[0], retailGoodsSkusPrice * (
                    #                             100 - retailSets[3] - retailSets[2] - subRatioList[1]) / 100,
                    #                                                      change_price[0]))
                    #         else:
                    #             self.session.commit()
                    #             change_price = self.session.execute(
                    #                 "select change_price from retail_user_frozen_price5_records where user_id = :userId and target_id = :targetId limit 1",
                    #                 params={"userId": referee_user[0], "targetId": orderId}).first()
                    #             if change_price[0] != retailGoodsSkusPrice * (100 - retailSets[3] - retailSets[2] - subRatioList[1]) / 100:
                    #                 print("{}分润差额2计算错误，预期{}，实际{}".format(referee_user[0], retailGoodsSkusPrice * (100 - retailSets[3] - retailSets[2] - subRatioList[1]) / 100, change_price[0]))

                    print("开始。。。。。")
                    self.session.commit()
                    retail_agent_id = self.session.execute(
                        "select retail_agent_id from retail_stores where id = :storeId limit 1",
                        params={"storeId": unboundStoreId}).first()
                    print("retailGoodsSkusPrice：", retailGoodsSkusPrice)
                    self.check_agencys_sharing(retail_agent_id[0], retailGoodsSkusPrice * retailSets[3] / 100 / 2,
                                               orderId, crossStore, bindStoreId)
                    print("结束。。。。。")


    # 购买商品
    def purchase_retail_order(self):

        userList = self.session.execute("select ru.user_id, u.mobile, ua.id, ru.bind_retail_store_id, ru.current_retail_store_id\
                            from retail_users ru\
                            inner join user_addresses ua on ru.user_id = ua.user_id\
                            inner join users u on u.id = ru.user_id\
                            where ru.is_real_name = 1 and ua.is_default = 1").fetchall()
        user = userList[random.randint(0, len(userList) - 1)]
        # user = userList[0]

        vip_level = self.session.execute("select vip_level from retail_vips where user_id = :userId limit 1",
                                         params={"userId": user[0]}).first()
        print("vip_level：", vip_level)

        requests.request("POST", url="https://test.api.vodeshop.com/api/sms/user-login", json={"mobile":user[1]})
        time.sleep(5)

        print("user：", user)
        userToken = requests.request("POST", url="https://test.api.vodeshop.com/api/auth/login/mobile", json={"mobile":user[1],"password":"","code":"111111"}).json()
        print("userToken：", userToken)
        self.token = userToken["data"]["token_type"] + " " + userToken["data"]["access_token"]

        goodList = requests.request("GET", url="https://test.api.vodeshop.com/api/retail/mall/index-goods?page="+str(random.randint(1,4)), headers={"x-request-from":"web","x-requested-with":"XMLHttpRequest","authorization":self.token}).json()["data"]["data"]
        good = goodList[random.randint(0,14)]
        print("good：", good)

        goodSkuList = requests.request("GET", url="https://test.api.vodeshop.com/api/retail/goods/show?retail_goods_id=" + str(good["id"]), headers={"x-request-from":"web","x-requested-with":"XMLHttpRequest","authorization":self.token}).json()["data"]["goods_skus"]
        if len(goodSkuList) == 1:
            self.goodSku = goodSkuList[0]
        elif len(goodSkuList) > 1:
            self.goodSku = goodList[random.randint(0, random.randint(0,len(goodSkuList) - 1))]
        print("goodSku：", self.goodSku)

        goodTotal = random.randint(2, 3)
        orderCheck = requests.request("POST", url="https://test.api.vodeshop.com/api/retail/check-order", headers={"x-request-from":"web","x-requested-with":"XMLHttpRequest","authorization":self.token}, json={"retail_goods_id":[good["id"]],"address_id":user[2]}).json()
        print("addr_carriage：", orderCheck["data"][0]["addr_carriage"])
        confirm = requests.request("POST", url="https://test.api.vodeshop.com/api/retail/order/confirm", headers={"x-request-from":"web","x-requested-with":"XMLHttpRequest","authorization":self.token}, json={"goods":[{"goods_sku_id":self.goodSku["id"],"goods_total":goodTotal}],"cart_id":[],"address_id":user[2]}).json()
        print("confirm：", confirm)
        order = requests.request("POST", url="https://test.api.vodeshop.com/api/retail/order/order", headers={"x-request-from":"web","x-requested-with":"XMLHttpRequest","authorization":self.token}, json={"goods":[{"goods_sku_id":self.goodSku["id"],"goods_total":goodTotal}],"cart_id":[],"address_id":user[2],"pay_type":1, "purchase_way":0}).json()
        print("order：", order)
        self.session.commit()
        retail_sets = self.session.execute("select credit_ratio, bind_store_ratio, current_store_ratio, agent_ratio from retail_sets limit 1").first()
        goods_sku = confirm["data"]["goods"][0]["items"][0]["goods_sku"]
        amount = (decimal.Decimal(goods_sku["price"]) + decimal.Decimal(orderCheck["data"][0]["addr_carriage"]) - decimal.Decimal(goods_sku["sub_price"])) * goodTotal
        print("amount：", amount)
        self.session.commit()
        order_sn = self.session.execute(
            "select order_sn from payments where user_id = :userId and targetable_id =:targetableId order by created_at desc limit 1", params={"userId":user[0], "targetableId": order["data"]["order_merge_id"]}).first()
        print("order_sn：", order_sn)

        res = requests.request("POST", url="https://test.api.vodeshop.com/api/v2/test/notifyyyy", data={"out_trade_no":order_sn[0], "trade_no":order_sn[0], "total_amount":"1"}).json()
        print("res：", res)
        time.sleep(10)

        self.session.commit()
        retail_goods_skus = self.session.execute(
            "select price, cost_price, platform_profit_price from retail_goods_skus rgs where id = :skuId limit 1", params={"skuId": self.goodSku["id"]}).first()

        subRatioList = []
        for num in range(1, 3):
            self.session.commit()
            sub_ratio = self.session.execute(
                "select sub_ratio from retail_vip_levels where vip_level = :vipLevel limit 1",
                params={"vipLevel": num}).first()
            subRatioList.append(sub_ratio[0])

        crossStore = False
        self.session.commit()
        retail_store_id = self.session.execute(
            "select ro.retail_store_id, rog.id\
                    from retail_orders ro\
                    inner join retail_order_goods rog on rog.retail_order_id = ro.id\
                    where ro.id = :orderId limit 1", params={"orderId": order["data"]["order_id"]}).first()
        if user[3] != retail_store_id[0]:
            crossStore = True

        union, partner = 1, 1
        if vip_level != None:
            proportion = 0

            if vip_level[0] == 1:
                union += 1
                proportion = subRatioList[0]

            elif vip_level[0] == 2:
                partner += 1
                proportion = subRatioList[1]

            elif vip_level[0] == 3:
                if crossStore == False:
                    proportion = 100 - retail_sets[3]
                else:
                    proportion = 100 - retail_sets[3] - retail_sets[2]

            if decimal.Decimal(good["sub_price"]) != (retail_goods_skus[0] - retail_goods_skus[1] - retail_goods_skus[2]) * \
                    proportion / 100:
                print("{}商品列表会员立减错误，预期{}，实际{}".format(good["id"],
                                                      (retail_goods_skus[0] - retail_goods_skus[1] - retail_goods_skus[2]) *
                                                      proportion / 100, decimal.Decimal(good["sub_price"])))

            if decimal.Decimal(confirm["data"]["sub_price"]) != (
                    retail_goods_skus[0] - retail_goods_skus[1] - retail_goods_skus[2]) * proportion / 100 * goodTotal:
                print("{}结算页面会员立减错误，预期{}，实际{}".format(good["id"], (
                        retail_goods_skus[0] - retail_goods_skus[1] - retail_goods_skus[2]) * proportion / 100 * goodTotal,
                                                      decimal.Decimal(confirm["data"]["sub_price"])))

        self.check_members_sharing(user[0], retail_store_id[1], amount - decimal.Decimal(orderCheck["data"][0]["addr_carriage"]) * goodTotal, subRatioList, (retail_goods_skus[0] - retail_goods_skus[1] - retail_goods_skus[2]) * goodTotal, retail_sets, 1, union, partner, crossStore, user[3], retail_store_id[0], vip_level)


if __name__ == "__main__":
    RetailOrderSharing().purchase_retail_order()










